<template>
	<view class="payOrder">
		<view class="money">
			<view class="top">
				<view style="color: #989898;">订单金额</view>
				<u-count-down :time="countDown" format="HH:mm:ss" @change="onChange"></u-count-down>
				<view><text>{{obj.orderTotal}}</text><text style="font-size: 22rpx;">元</text></view>
			</view>
			<view class="top">
				<view style="color: #989898;">优惠金额</view>
				<view style="color: #fbc21a;"><text>-￥{{obj.discountTotal}}</text></view>
			</view>
			<view class="top" v-if="obj.ruleDiscountTotal > 0">
				<view style="color: #989898;">砍价金额</view>
				<view style="color: red;"><text>-￥{{obj.ruleDiscountTotal}}</text></view>
			</view>
			<view class="middle">
				<view style="padding-bottom: 20rpx;font-size: 22rpx;color: #989898;">支付金额</view>
				<view><text style="font-size: 32rpx;color: #3E56BA;">￥</text><text
						style="font-size: 48rpx;color: #3E56BA;">{{obj.total}}</text></view>
			</view>
			<view class="bottom" v-if="shareHand">
				<u-button open-type="share" type="primary" text="发起砍价"></u-button>
			</view>
		</view>
		<view class="pay">
			<view class="title">支付方式</view>
			<view class="content">
				<view v-if="obj.total !== 0"
					style="display: flex;justify-content: space-between;padding: 32rpx 0;border-bottom: 1px solid #F4F4F4;"
					@click="toPay(orderNo)">
					<view style="display: flex;align-items: center;">
						<image style="width: 42rpx;padding: 0 14rpx;" src="http://applet.atptest.com/23042301.png"
							mode="widthFix"></image>
						<view style="font-size: 28rpx;">微信</view>
					</view>
					<view>
						<u-icon name="arrow-right"></u-icon>
					</view>
				</view>

				<view style="display: flex;justify-content: space-between;padding: 32rpx 0;"
					@click="toPaySubmit({orderNo:orderNo})">
					<view style="display: flex;align-items: center;">
						<image style="width: 42rpx;padding: 0 14rpx;" src="http://applet.atptest.com/23042302.png"
							mode="widthFix"></image>
						<view style="font-size: 28rpx;"><text style="padding-right:30rpx;">余额</text><text
								style="color: #868686;font-size: 24rpx;">账户余额：{{obj.balance}}</text></view>
					</view>
					<view>
						<u-icon name="arrow-right"></u-icon>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		PaymentOrderDetails,
		PaySubmit
	} from '../../../api/balance.js'
	import {
		GetPayPermTest222
	} from '../../../api/user.js'
	import {
		shareFlag
	} from "../../../api/bargain.js"
	export default {
		data() {
			return {
				obj: {
					list: []
				},
				// 订单号
				orderNo: '',
				// 倒计时
				countDown: 86400000,
				timer: '',
				shareHand: false, //是否发起砍价
				payFlag: true
			};
		},
		onLoad: function(option) {
			this.orderNo = option.i
		},
		onShow() {
			// 根据订单编号获取订单支付详情
			PaymentOrderDetails({
				orderNo: this.orderNo
			}).then((res) => {
				this.obj = res
				this.countDown = 86400000 - (parseInt(new Date() - new Date(this.obj.createTime)))
			})

			//能否发起砍价
			this.shareRule()
		},
		onHide() {
			clearTimeout(this.timer)
		},
		methods: {

			//能否发起砍价
			async shareRule() {
				const res = await shareFlag()
				this.shareHand = res.hand
			},

			// 去优惠券选择界面
			toDiscount() {
				uni.navigateTo({
					url: `/pagesA/pages/discountSelect/discountSelect?i=${this.obj.id}`
				})
			},

			//自定义分享 当前只针对自定义分享时触发的情况，如果时点击小程序自带三个点分享页面的，需根据e来判断点击类型进行分享操作！
			async onShareAppMessage(e) {
				return {
					title: '点击为好友砍价',
					path: `/pagesB/pages/bargain/bargain?orderId=${this.obj.id}`,
					desc: '将订单转发给好友进行砍价',
					imageUrl: 'http://applet.atptest.com/JlrzC9db1719997453657.jpg?v=1719997454121',
				}
			},

			// 余额支付提交
			toPaySubmit(obj) {
				uni.showModal({
					title: '提示',
					content: '确认余额支付吗？',
					success(res) {
						if (res.confirm) {
							uni.showLoading({
								title: '加载中'
							})
							PaySubmit(obj).then((res) => {
								if (res === true) {
									uni.hideLoading();
									uni.showToast({
										duration: 3000,
										title: '支付成功！'
									})
									setTimeout(() => {
										uni.reLaunch({
											url: '/pagesB/pages/order/order?i=3'
										})
									}, 1000)
								}
							})
						} else if (res.cancel) {
							console.log('取消');
						}
					}
				})
			},

			// 获微信支付数据
			toPay(e) {
				if (this.payFlag) { 
					this.payFlag = false //防止重复点击
					GetPayPermTest222({
						orderNo: e
					}).then((res) => {
						const _that = this
						uni.requestPayment({
							provider: 'wxpay',
							timeStamp: res.timeStamp,
							nonceStr: res.nonceStr,
							package: res.package,
							signType: res.signType,
							paySign: res.paySign,
							success: function(res) {
								// 成功回调
								uni.showToast({
									title: '下单成功！！',
									duration: 2000,
								});
								_that.payFlag = true
								uni.reLaunch({
									url: '/pagesB/pages/order/order?i=3'
								})
							},
							fail: function(err) {
								uni.showToast({
									title: '取消支付',
									duration: 2000,
									icon: "error"
								});
								_that.payFlag = true
								console.log('fail:' + JSON.stringify(err));
							}
						});
					})
				}
			},
			//倒计时
			onChange(e) {
				if (e.days == 0 && e.hours == 0 && e.minutes == 0 && e.seconds == 0 && e.milliseconds == 0) {
					uni.showToast({
						title: "订单已过期，请重新下单",
						icon: 'none'
					})

					this.timer = setTimeout(function() {
						uni.hideLoading();
						uni.redirectTo({
							url: '/pages/test/test'
						})
					}, 2000);
				}
			}
		},
	}
</script>

<style lang="scss" scoped>
	.payOrder {
		padding: 30rpx;
		height: 100vh;
		background-color: #f5f5f5;

		.money {
			background-color: #fff;
			border-radius: 12rpx;

			// box-sizing: border-box;
			.top {
				display: flex;
				justify-content: space-between;
				padding: 26rpx 20rpx 30rpx;
			}

			.middle {
				display: flex;
				flex-direction: column;
				justify-content: center;
				align-items: center;
				border-top: 1px solid #F4F4F4;
				border-bottom: 1px solid #F4F4F4;
				padding: 30rpx 0;
			}
		}

		.bottom {
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 28rpx 20rpx;

			.bargain_btn {
				background-color: #3e56ba;
				color: #fff;
			}
		}

		.pay {
			margin-top: 30rpx;
			background-color: #fff;
			border-radius: 12rpx;

			.title {
				padding: 20rpx;
				color: #3E56BA;
			}

			.content {}
		}
	}

	/deep/ .u-count-down__text {
		color: #BB645A !important;
	}
</style>